<?php

/**
 *  @FileComment            [前台商品内容页控制器]
 *  @ProjectName             项目名称：ShopFrame
 *  @FileName                   文件名称：ContentController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-4 21:58:56
 *  *
 */
class ContentController extends CommonController {

        public function index() {
                //当前在哪个分类
                $gid = Q('get.gid', 0, 'intval');
                //品牌信息
                $goodsInfo = K('Goods')->get_one_data($gid);
                $brandInfo = K('Brand')->get_o_data(array('bid' => $goodsInfo['brand_bid']));
                $goodsAttrModel = K('GoodsAttr');
                $add = $goodsAttrModel->get_field('gaid in(' . $goodsInfo['comb'] . ')');
                $addprice = 0;
                //附加价格信息
                if (!$add) {
                        $add = array();
                }
                foreach ($add as $v) {
                        $addprice +=$v;
                }
                $goodsListModel = K('GoodsList');
                $goodsList = $goodsListModel->get_o_data(array('combattrid' => $goodsInfo['comb']));
                $this->assign('goodslist', $goodsList);
                $goodsInfo['jdprice']+=$addprice;
                //商品细节商品规格和属性等等
                $goodsInfo['comb'] = explode(',', $goodsInfo['comb']);
                $goodsDetail = K('GoodsDetail')->get_one_data($gid);
                $goodStand = K('GoodsAttrTypeAttrView')->get_all_data('goods_gid=' . $gid . ' and class=1 and is_select=1', 'taname');
                foreach ($goodStand as $k => $v) {
                        $goodStand[$k]['taval'] = $goodsAttrModel->get_all_data(array('type_attr_taid' => $v['taid'], 'goods_gid' => $gid), 'ganame');
                }
                $goodAttr = K('GoodsAttrTypeAttrView')->get_data($gid, 0);
                $this->assign('goodsAttr', $goodAttr);
                $this->cate($goodsInfo['category_cid']);
                $this->assign('goodsInfo', $goodsInfo);
                $this->assign('goodStand', $goodStand);
                //商品图
                $goodsDetail['thumb'] = unserialize($goodsDetail['thumb']);
                $thumbFirst = current($goodsDetail['thumb']);
                $this->assign('thumbFirst', $thumbFirst);
                $this->assign('goodsDetail', $goodsDetail);
                $this->assign('brandInfo', $brandInfo);
                //相关信息、相关分类，相关品牌
                $pInfo = K('GoodsCategoryView')->get_one_data('gid=' . $gid);
                $pid = $pInfo['pid'];
                $cInfo = K('category')->get_pid_data(array('pid' => $pid));
                $this->assign('cInfo', $cInfo);
                $tid = $pInfo['type_tid'];
                $bInfo = K('BrandTypeView')->get_brand_data(array('type_tid' => $tid));
                $this->assign('bInfo', $bInfo);
                //评价信息
                $commentInfo = K('OrderOrderListCommentUserView')->get_data($gid);
                $this->assign('commentInfo', $commentInfo);
                $this->display();
        }

        public function ajaxChoose() {
                //选择规格的时候ajax异步的动作
                $comb = Q('post.comb');
                $comb = rtrim($comb, ',');
                $goodsList = K('GoodsList')->get_o_data('combattrid="' . $comb . '"');
                $temp = array();
                //返回数据，返回当前货品的信息
                if ($goodsList) {
                        $goodsAttrModel = K('GoodsAttr');
                        $add = $goodsAttrModel->get_field('gaid in(' . $goodsList['combattrid'] . ')');
                        $addprice = 0;
                        foreach ($add as $v) {
                                $addprice +=$v;
                        }
                        $gid = $goodsList['goods_gid'];
                        $price = K('goods')->get_one_data($gid);
                        $price = $price['jdprice'];
                        $temp['jdprice'] = $addprice + $price;
                        $temp['status'] = 1;
                        $temp['stock'] = $goodsList['stock'];
                        $temp['number'] = $goodsList['number'];
                        $temp['glid'] = $goodsList['glid'];
                        if ($goodsList['number'] == 0) {
                                $temp['number'] = 0;
                        }
                } else {
                        $temp['jdprice'] = 0;
                        $temp['status'] = 1;
                        $temp['stock'] = 0;
                        $temp['number'] = 0;
                }
                $this->ajax($temp);
        }

}
